package com.device.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.device.Constant.BaseConstant;
import com.device.Entity.Device;
import com.device.Entity.Lab;
import com.device.Entity.Sbapply;
import com.device.Entity.User;
import com.device.Exception.BaseException;
import com.device.Mapper.DeviceMapper;
import com.device.Mapper.LabMapper;
import com.device.Mapper.SbapplyMapper;
import com.device.Mapper.UserMapper;
import com.device.Service.SbapplyService;
import com.device.Service.UserService;
import com.device.common.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class SbapplyServiceImpl extends ServiceImpl<SbapplyMapper, Sbapply> implements SbapplyService {
    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private LabMapper labMapper;
    @Resource
    private SbapplyMapper sbapplyMapper;

    public void apply(Integer deviceid, Integer uid, String remark) {

        Device device = deviceMapper.selectById(deviceid);
        if (device == null) {
            throw new BaseException("该设备不存在");
        }
        LambdaQueryWrapper<Sbapply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Sbapply::getDeviceid, deviceid);
        lambdaQueryWrapper.eq(Sbapply::getStatus, BaseConstant.ZERO);
        lambdaQueryWrapper.eq(Sbapply::getUid, uid);

        Sbapply sbapply = sbapplyMapper.selectOne(lambdaQueryWrapper);
        if (sbapply != null) {
            throw new BaseException("您已提交过该申请，请勿重复提交");
        }
        Sbapply sbapply1 = new Sbapply();
        Lab lab = labMapper.selectById(device.getSysid());
        //设置实验室管理员id
        sbapply1.setDglyid(lab.getGlyid());
        sbapply1.setCreatetime(new Date());
        sbapply1.setGlystatus(0);
        sbapply1.setDeviceid(deviceid);
        sbapply1.setUid(uid);
        sbapply1.setRemark(remark);
        sbapply1.setType(BaseConstant.ONE);
        sbapply1.setStatus(BaseConstant.ZERO);
        sbapplyMapper.insert(sbapply1);


    }
    public   void repaire(Integer id, Integer uid, String remark){
     Sbapply sbapply=sbapplyMapper.selectById(id);
     if(sbapply == null){
         throw new BaseException("该使用申请不存在");
     }

        Device device = deviceMapper.selectById(sbapply.getDeviceid());
        if (device == null) {
            throw new BaseException("该设备不存在");
        }
        LambdaQueryWrapper<Sbapply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Sbapply::getDeviceid, device.getId());
        lambdaQueryWrapper.eq(Sbapply::getStatus, BaseConstant.ZERO);
        lambdaQueryWrapper.eq(Sbapply::getUid, uid);

        Sbapply sbapply1 = sbapplyMapper.selectOne(lambdaQueryWrapper);
        if (sbapply1 != null) {
            throw new BaseException("您已提交过该维修申请，请勿重复提交");
        }
        Sbapply sbapply2 = new Sbapply();
        Lab lab = labMapper.selectById(device.getSysid());
        //设置实验室管理员id
        sbapply2.setDglyid(lab.getGlyid());
        sbapply2.setCreatetime(new Date());
        sbapply2.setGlystatus(BaseConstant.ZERO);
        sbapply2.setDeviceid(device.getId());
        sbapply2.setUid(uid);
        sbapply2.setRemark(remark);
        sbapply2.setType(BaseConstant.TWO);
        sbapply2.setStatus(BaseConstant.ZERO);
        sbapplyMapper.insert(sbapply2);

    }

    public PageResult getpage(Integer pageNo, Integer pageSize, Integer status, Integer uid) {
        PageResult pageResult = new PageResult();
        IPage<Sbapply> page = new Page<>(pageNo, pageSize);
        IPage<Sbapply> list = sbapplyMapper.getpage(page, status, uid,BaseConstant.ONE);
        pageResult.setTotal(list.getTotal());
        pageResult.setRecords(list.getRecords());
        return pageResult;


    }

    public void editapply(Integer id, Integer uid, String remark) {
        LambdaQueryWrapper<Sbapply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Sbapply::getId, id);
        Sbapply sbapply = sbapplyMapper.selectOne(lambdaQueryWrapper);
        if (sbapply == null) {
            throw new BaseException("该申请不存在");
        }
        sbapply.setRemark(remark);
        sbapplyMapper.updateById(sbapply);

    }

    public void resetapply(Integer id) {
        LambdaQueryWrapper<Sbapply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Sbapply::getId, id);
        Sbapply sbapply = sbapplyMapper.selectOne(lambdaQueryWrapper);
        if (sbapply == null) {
            throw new BaseException("该申请不存在");
        }
        sbapply.setStatus(BaseConstant.TWO);
        sbapply.setGlystatus(BaseConstant.THREE);
        sbapplyMapper.updateById(sbapply);
    }

    public PageResult getpage1(Integer pageNo, Integer pageSize, Integer status, Integer uid) {
        PageResult pageResult = new PageResult();
        IPage<Sbapply> page = new Page<>(pageNo, pageSize);
        IPage<Sbapply> list = sbapplyMapper.getpage1(page, status, uid,BaseConstant.ONE);
        pageResult.setTotal(list.getTotal());
        pageResult.setRecords(list.getRecords());
        return pageResult;
    }
    public PageResult getpage2(Integer pageNo, Integer pageSize, Integer status, Integer uid) {
        PageResult pageResult = new PageResult();
        IPage<Sbapply> page = new Page<>(pageNo, pageSize);
        IPage<Sbapply> list = sbapplyMapper.getpage(page, status, uid,BaseConstant.TWO);
        pageResult.setTotal(list.getTotal());
        pageResult.setRecords(list.getRecords());
        return pageResult;
    }
    public PageResult getpage3(Integer pageNo, Integer pageSize, Integer status, Integer uid) {
        PageResult pageResult = new PageResult();
        IPage<Sbapply> page = new Page<>(pageNo, pageSize);
        IPage<Sbapply> list = sbapplyMapper.getpage1(page, status, uid,BaseConstant.TWO);
        pageResult.setTotal(list.getTotal());
        pageResult.setRecords(list.getRecords());
        return pageResult;
    }
public   PageResult getpage4(Integer pageNo, Integer pageSize, Integer status,  Integer uid){
    PageResult pageResult = new PageResult();
    IPage<Sbapply> page = new Page<>(pageNo, pageSize);
    IPage<Sbapply> list = sbapplyMapper.getpage1(page, status, uid,BaseConstant.TWO);
    pageResult.setTotal(list.getTotal());
    pageResult.setRecords(list.getRecords());
    return pageResult;
}

    public void dglyapply(Integer id, Integer glystatus) {
        LambdaQueryWrapper<Sbapply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Sbapply::getId, id);
        Sbapply sbapply = sbapplyMapper.selectOne(lambdaQueryWrapper);
        if (sbapply == null) {
            throw new BaseException("该申请不存在");
        }

        if (glystatus.equals(BaseConstant.TWO)) {
            sbapply.setStatus(BaseConstant.THREE);
            sbapply.setGlystatus(BaseConstant.TWO);
            sbapplyMapper.updateById(sbapply);
        } else {

            sbapply.setGlystatus(BaseConstant.ONE);
            sbapplyMapper.updateById(sbapply);

        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void cglyapply(Integer id, Integer status) {
        LambdaQueryWrapper<Sbapply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Sbapply::getId, id);
        Sbapply sbapply = sbapplyMapper.selectOne(lambdaQueryWrapper);
        if (sbapply == null) {
            throw new BaseException("该申请不存在");
        }
        if (status.equals(BaseConstant.THREE)) {
            sbapply.setStatus(BaseConstant.THREE);
            sbapplyMapper.updateById(sbapply);
        } else {

            sbapply.setStatus(BaseConstant.ONE);
            Device device = deviceMapper.selectById(sbapply.getDeviceid());
            device.setStatus(BaseConstant.ZERO);
            deviceMapper.updateById(device);
            sbapplyMapper.updateById(sbapply);

        }
    }
public     void cglywxapply(Integer id, Integer status){
    LambdaQueryWrapper<Sbapply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    lambdaQueryWrapper.eq(Sbapply::getId, id);
    Sbapply sbapply = sbapplyMapper.selectOne(lambdaQueryWrapper);
    if (sbapply == null) {
        throw new BaseException("该申请不存在");
    }
    if (status.equals(BaseConstant.THREE)) {
        sbapply.setStatus(BaseConstant.THREE);
        sbapplyMapper.updateById(sbapply);
    } else {

        sbapply.setStatus(BaseConstant.ONE);
        Device device = deviceMapper.selectById(sbapply.getDeviceid());
        device.setStatus(BaseConstant.TWO);
        deviceMapper.updateById(device);
        sbapplyMapper.updateById(sbapply);

    }
}
    public void returns(Integer id) {
        LambdaQueryWrapper<Sbapply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Sbapply::getId, id);
        Sbapply sbapply = sbapplyMapper.selectOne(lambdaQueryWrapper);
        if (sbapply == null) {
            throw new BaseException("该申请不存在");
        }
        Device device = deviceMapper.selectById(sbapply.getDeviceid());
        device.setStatus(BaseConstant.ONE);
        sbapply.setStatus(BaseConstant.Four);
        deviceMapper.updateById(device);
        sbapplyMapper.updateById(sbapply);
    }

    public void delete(Integer id) {
        LambdaQueryWrapper<Sbapply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Sbapply::getId, id);
        Sbapply sbapply = sbapplyMapper.selectOne(lambdaQueryWrapper);
        if (sbapply == null) {
            throw new BaseException("该申请不存在");
        }
        sbapplyMapper.deleteById(id);

    }
}
